package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.GoodsProperties;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesAddParam;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsPropertiesUpdateParam;
import com.hoshiicloud.goods.service.GoodsPropertiesService;
import com.hoshiicloud.goods.utils.UserInfoUtil;
import com.hoshiicloud.goods.vo.GoodsPropertiesPageVo;
import com.hoshiicloud.goods.vo.GoodsPropertiesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品属性 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-properties")
@Api(value = "商品属性", tags = {"商品属性"})
public class GoodsPropertiesController {
    @Autowired
    private GoodsPropertiesService goodsPropertiesService;

    @ApiOperation(value = "分页查询商品属性")
    @PostMapping("/getPage")
    public Response<Page<GoodsPropertiesPageVo>> page(@RequestBody GoodsPropertiesQueryParam param) {
        try {
            QueryWrapper<GoodsProperties> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(GoodsProperties::getCompanyId, param.getCompanyId())
                    .like(StringUtils.isNotBlank(param.getPropertyName()), GoodsProperties::getPropertyName, param.getPropertyName())
                    .eq((param.getPropertyType() != null), GoodsProperties::getPropertyType, param.getPropertyType())
                    .like(StringUtils.isNotBlank(param.getPropertyValue()), GoodsProperties::getPropertyValue, param.getPropertyValue())
                    .eq( GoodsProperties::getDeleted, 0);
            Page<GoodsPropertiesPageVo> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "GoodsPropertiesQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(goodsPropertiesService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品属性异常", e);
            return Response.failed("分页查询商品属性异常");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品属性")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsPropertiesService.getById(id));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增商品属性")
    public Response save(@RequestBody GoodsPropertiesAddParam param) {
        GoodsProperties entity;
        try {
            entity = BeanUtils.covertType(param, GoodsProperties.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsPropertiesService.save(entity));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品属性")
    public Response update(@RequestBody GoodsPropertiesUpdateParam param) {
        // Bean数据转换
        GoodsProperties entity;
        try {
            entity = BeanUtils.covertType(param, GoodsProperties.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsPropertiesService.updateById(entity));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品属性")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(goodsPropertiesService.deleteById(id));
    }


    @ApiOperation(value = "根据id集合查询商品属性")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsPropertiesService.getByIds(ids, StringUtils.join(ids, "")));
    }

    @GetMapping("/getProperByCateId")
    public Response<List<GoodsPropertiesVo>> getProperByCateId(@RequestParam("categoryId") Long categoryId,@RequestParam("companyId") Long companyId) {
        return  Response.data(goodsPropertiesService.getPropertiesVoList(categoryId, companyId));
    }
}
